route.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { getServerSession } from "next-auth";
  3. import { authOptions } from "@/lib/auth";
  4. import { prisma } from "@/lib/prisma";
  5. // GET /api/appointments/[id]
  6. export async function GET(
  7. request: NextRequest,
  8. { params }: { params: Promise<{ id: string }> }
  9. ) {
  10. try {
  11. const { id } = await params;
  12. const session = await getServerSession(authOptions);
  13. if (!session?.user?.email) {
  14. return NextResponse.json({ error: "No autorizado" }, { status: 401 });
  15. }
  16. const user = await prisma.user.findUnique({
  17. where: { email: session.user.email },
  18. });
  19. if (!user) {
  20. return NextResponse.json({ error: "Usuario no encontrado" }, { status: 404 });
  21. }
  22. const appointment = await prisma.appointment.findUnique({
  23. where: { id },
  24. include: {
  25. paciente: {
  26. select: {
  27. id: true,
  28. name: true,
  29. lastname: true,
  30. email: true,
  31. profileImage: true,
  32. phone: true,
  33. },
  34. },
  35. medico: {
  36. select: {
  37. id: true,
  38. name: true,
  39. lastname: true,
  40. email: true,
  41. profileImage: true,
  42. },
  43. },
  44. },
  45. });
  46. if (!appointment) {
  47. return NextResponse.json({ error: "Cita no encontrada" }, { status: 404 });
  48. }
  49. // Validar acceso
  50. const canAccess =
  51. appointment.pacienteId === user.id ||
  52. appointment.medicoId === user.id ||
  53. user.role === "ADMIN";
  54. if (!canAccess) {
  55. return NextResponse.json({ error: "No autorizado" }, { status: 403 });
  56. }
  57. return NextResponse.json(appointment);
  58. } catch (error) {
  59. console.error("Error al obtener cita:", error);
  60. return NextResponse.json({ error: "Error al obtener cita" }, { status: 500 });
  61. }
  62. }
  63. // PATCH /api/appointments/[id] - Cancelar cita (paciente)
  64. export async function PATCH(
  65. request: NextRequest,
  66. { params }: { params: Promise<{ id: string }> }
  67. ) {
  68. try {
  69. const { id } = await params;
  70. const session = await getServerSession(authOptions);
  71. if (!session?.user?.email) {
  72. return NextResponse.json({ error: "No autorizado" }, { status: 401 });
  73. }
  74. const user = await prisma.user.findUnique({
  75. where: { email: session.user.email },
  76. });
  77. if (!user) {
  78. return NextResponse.json({ error: "Usuario no encontrado" }, { status: 404 });
  79. }
  80. const appointment = await prisma.appointment.findUnique({
  81. where: { id },
  82. });
  83. if (!appointment) {
  84. return NextResponse.json({ error: "Cita no encontrada" }, { status: 404 });
  85. }
  86. // Solo el paciente puede cancelar
  87. if (appointment.pacienteId !== user.id) {
  88. return NextResponse.json({ error: "No autorizado" }, { status: 403 });
  89. }
  90. const updated = await prisma.appointment.update({
  91. where: { id },
  92. data: { estado: "CANCELADA" },
  93. include: {
  94. paciente: {
  95. select: {
  96. id: true,
  97. name: true,
  98. lastname: true,
  99. email: true,
  100. profileImage: true,
  101. },
  102. },
  103. medico: {
  104. select: {
  105. id: true,
  106. name: true,
  107. lastname: true,
  108. email: true,
  109. profileImage: true,
  110. },
  111. },
  112. },
  113. });
  114. return NextResponse.json(updated);
  115. } catch (error) {
  116. console.error("Error al cancelar cita:", error);
  117. return NextResponse.json({ error: "Error al cancelar cita" }, { status: 500 });
  118. }
  119. }